from sanic import Sanic
from sanic import response

from api import api
from page import page


app = Sanic()

app.static('/resource', './resource')  # 这里注册了static url为/static 实际物理路径为/var/tmp

app.blueprint(api)
app.blueprint(page)

@app.route('/')
async def home(request):
    # generate a URL for the endpoint `toolsView`, `my_page_v1` is Blueprint name
    url = app.url_for('my_page_v1.toolsView')
    # the URL is `/toolsView`, redirect to it
    return response.redirect(url)

@app.route('/resource/<filename>')
async def resource_handle(request, filename):
    return await response.file('./resource/'+filename)

@app.middleware('response') 
async def allowCORS(request, response): #跨域
    response.headers["Access-Control-Allow-Origin"] = "*"
    response.headers["Access-Control-Allow-Methods"] = "GET,POST"

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000)